package selevert;

import java.sql.*;
import java.util.Scanner;

public class Data {
    public static void main(String[] args) {
        while (true) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入你想要的功能:(1、查询所有数据 2、添加数据 3、修改数据 4、删除数据 5、退出)");
            int num = scanner.nextInt();
            switch (num) {
                case 1:
                    new Select().select();
                    break;
                case 2:
                    new Add().add();
                    break;
                case 3:
                    new Updata().updata();
                    break;
                case 4:
                    new Delect().delect();
                    break;
            }
            if (num == 5) {
                break;
            }
        }
    }

    static class Select {
        void select() {
            Connection conn = null;
            Statement stmt = null;
            ResultSet rs = null;

            try {

                String user = "root";
                String pwd = "xflovexz";
                // 加载MySQL驱动类
                Class.forName("com.mysql.jdbc.Driver");

                // 创建连接
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?Unicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true", user, pwd);

                // 创建Statement对象
                stmt = conn.createStatement();

                // 执行查询操作
                rs = stmt.executeQuery("SELECT * FROM sutdent");

                // 遍历结果集
                while (rs.next()) {
                    System.out.println(rs.getInt("id") + "\t" + rs.getString("name") + "\t" + rs.getString("gender"));
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                // 释放资源
                try {
                    if (rs != null) {
                        rs.close();
                    }
                    if (stmt != null) {
                        stmt.close();
                    }
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static class Add {
        void add() {
            try {
                String user = "root";
                String pwd = "xflovexz";
                Class.forName("com.mysql.jdbc.Driver");
                Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?Unicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true", user, pwd);
                Statement statement = connection.createStatement();
                int resultSet = statement.executeUpdate("insert into sutdent values (4,'小明','男')");
                if (pwd != null) {
                    System.out.println("添加成功");
                } else {
                    System.out.println("添加失败");
                }
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static class Updata {
        void updata() {
            try {
                String user = "root";
                String pwd = "xflovaxz";
                Connection connection = DriverManager.getConnection("jdbc:mysql:///student_db?Unicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true", user, pwd);
                Statement statement = connection.createStatement();
                int resultSet = statement.executeUpdate("update sutdent set name='小明' where id=2");
                if (pwd != null) {
                    System.out.println("修改成功");
                } else {
                    System.out.println("修改失败");
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }


    public static class Delect {
        void delect() {
            try {
                String user = "root";
                String pwd = "xflovexz";
                Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_db?Unicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true", user, pwd);
                Statement statement = connection.createStatement();
                int resultSet = statement.executeUpdate("delete from sutdent where id=4 ");
                if (pwd != null) {
                    System.out.println("删除成功");
                } else {
                    System.out.println("删除失败");
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }


}

